﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace DataBaseTest
{
    // 表头处理
    [SugarTable("Student")]
    public class Student
    {
        [SugarColumn(ColumnName = "Name")]
        public string Name { set; get; }

        [SugarColumn(ColumnName = "Age")]
        public int Age { set; get; }
    }

    class Program
    {

        public SqlSugarClient Client { get; }
        static void Main(string[] args)
        {
            var sc = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString      = "Server = 127.0.0.1; database = test; user = root; password = root",
                IsAutoCloseConnection = true,                  // 自动释放数据务，如果存在事务，在事务结束后释放
                DbType                = DbType.MySql,
                InitKeyType           = InitKeyType.Attribute  //从实体特性中读取主键自增列信息
            });

            sc.CodeFirst.InitTables(typeof(Student));

            //增
            sc.Insertable(new Student() { Name = "lilei", Age = 18 }).ExecuteCommand();
            sc.Insertable(new Student() { Name = "hanmeimei", Age = 17 }).ExecuteCommand();
            sc.Insertable(new Student() { Name = "Tom", Age = 20 }).ExecuteCommand();
            sc.Insertable(new Student() { Name = "asd1", Age = 19 }).ExecuteCommand();
            sc.Insertable(new Student() { Name = "asd2", Age = 19 }).ExecuteCommand();
            sc.Insertable(new Student() { Name = "asd3", Age = 19 }).ExecuteCommand();

            //查全部
            var list = sc.Queryable<Student>().ToList();

            //新建一个对象,用来替换内容
            var s2 = new Student() { Name = "lilei", Age = 9999 };
            //改
            sc.Updateable(s2).Where(it => it.Name == "lilei").ExecuteCommand();

            //删
            sc.Deleteable<Student>().Where(it => it.Name == "Tom").ExecuteCommand();

            Console.WriteLine("\n插入成功");
            Console.ReadLine();

            ////用来打印Sql方便你调试
            //Client.Aop.OnLogExecuting = (sql, pars) => {
            //    Console.WriteLine(sql + "\r\n" +
            //    Client.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            //    Console.WriteLine();
            //};
            //Client.CodeFirst.InitTables(typeof(Dept), typeof(Person), typeof(Employee));
            //Client.Aop.OnLogExecuting = (sql, paramters) => {
            //    Console.WriteLine(sql);
            //};


#if true3
        /*查询*/
        var list = db.Queryable<StudentModel>().ToList();                              //查询所有
        var getById = db.Queryable<StudentModel>().InSingle(1);                        //根据主键查询
        var getByWhere = db.Queryable<StudentModel>().Where(it => it.Id == 1).ToList();//根据条件查询
        var total = 0;
        var getPage = db.Queryable<StudentModel>().Where(it => it.Id == 1).ToPageList(1, 2, ref total);//根据分页查询
                                                                                                       //多表查询用法 http://www.codeisbug.com/Doc/8/1124

        /*插入*/
        var data = new Student() { Name = "jack" };
        db.Insertable(data).ExecuteCommand();
        //更多插入用法 http://www.codeisbug.com/Doc/8/1130

        /*更新*/
        var data2 = new Student() { Id = 1, Name = "jack" };
        db.Updateable(data2).ExecuteCommand();
        //更多更新用法 http://www.codeisbug.com/Doc/8/1129

        /*删除*/
        db.Deleteable<StudentModel>(1).ExecuteCommand();
#endif

        }
        public SimpleClient<T> CreateClient<T>() where T : class, new()
        {
            return Client.GetSimpleClient<T>();
        }
    }
}
